/**
 * 项目名称：MSH
 * 包名称：com.mt.common.sys.service
 * 文件名称：UserServiceImpl.java
 */
package com.mt.system.serviceImpl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import com.mt.component.context.ContextFactory;
import com.mt.component.message.Message;
import com.mt.system.dao.UserDao;
import com.mt.system.domain.User;
import com.mt.system.service.UserService;
import com.mt.utils.StringUtils;

import flex.messaging.FlexContext;

/**
 * 类名称：UserServiceImpl <br>
 * 类描述： <br>
 * 创建人： 马腾 <br>
 * 创建时间：2012-10-17 下午11:02:04 <br>
 */
@Service("UserService")
@RemotingDestination(channels = {"my-amf"})
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserDao userDao;
	
	@RemotingInclude
	@Override
	public User addUser(User user) {
		user.setOperateTime(new Date());
		return userDao.addUser(user);
	}

	@RemotingInclude
	@Override
	public void deleteBatchUser(List<User> users) {
		for (User user : users) {
			User temp = userDao.getUserById(user.getId());
			if(temp != null){
				temp.setStatus(0);
				userDao.updateUser(temp);
			}
		}
	}

	@RemotingInclude
	@Override
	public void updateUser(User user) {
		user.setOperateTime(new Date());
		userDao.updateUser(user);
	}

	@RemotingInclude
	@Override
	public List<User> getUsers(User userDto) {
		return userDao.getUsers(userDto);
	}

	@RemotingInclude
	@Override
	public int getCount(User userDto) {
		return userDao.getCount(userDto);
	}

	@Override
	@RemotingInclude
	public Message login(String name, String password , String validate) {
		Object object = FlexContext.getFlexSession().getAttribute("validate");
		String val = object == null ? null : object.toString();
		if(StringUtils.isEmpty(val)){
			return new Message(Message.FAULT, ContextFactory.getInstance().getMessage("10001"));
		}
		if(!val.equals(validate)){
			return new Message(Message.FAULT, ContextFactory.getInstance().getMessage("10002"));
		}
		User user = userDao.getUserByName(name);
		if(user == null){
			return new Message(Message.FAULT, ContextFactory.getInstance().getMessage("10003"));
		}
		if(!user.getPassword().equals(password)){
			return new Message(Message.FAULT, ContextFactory.getInstance().getMessage("10004"));
		}
		return new Message(Message.SUCCESS , user);
	}
}
